package com.community.web.controller.fq;

import com.community.common.annotation.Log;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.core.page.TableDataInfo;
import com.community.common.enums.BusinessType;
import com.community.common.utils.DateUtils;
import com.community.common.utils.SecurityUtils;
import com.community.common.utils.StringUtils;
import com.community.domain.StudentEntity;
import com.community.domain.fq.csd_parking;
import com.community.service.HouseService;
import com.community.service.fq.ParkService;
import com.community.domain.HouseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static com.community.common.utils.PageUtils.startPage;
@RestController
@RequestMapping("/park")
public class ParkController extends BaseController {
    @Autowired
    private ParkService parkService;
    @Autowired
    private HouseService houseService;
//    查询
    @PreAuthorize("@ss.hasPermi('park:park:list')")
    @GetMapping("/list")
    public TableDataInfo list(csd_parking parking) {
        startPage();
        List<csd_parking> list=parkService.selectParkList(parking);
        return getDataTable(list);
    }
//    删除
    @PreAuthorize("@ss.hasPermi('park:park:remove')")
    @DeleteMapping("/{id}")
    public AjaxResult delPark(@PathVariable String id) {
        if(StringUtils.isEmpty(id)){
            return AjaxResult.error("参数为空");
        }
        return toAjax(parkService.delPark(Integer.valueOf(id)));
    }
//增加

    @PreAuthorize("@ss.hasPermi('park:park:add')")
    @Log(title = "增加", businessType = BusinessType.INSERT)
    @PostMapping("/addPark")
    public AjaxResult addPark(@Validated @RequestBody csd_parking park) {
        String houseNum=park.getHouseNum();
        if(houseService.checkHouseNum(houseNum)==null)
        { return error("门牌号'"+houseNum+"'不存在");}
        else{
            String s1=park.getCode();
            String s2=park.getParkingLotCode();
            String s3=park.getParkingLotName();
            if(parkService.checcode(s1,s2,s3)!=null){
                return error("新增车位,停车场编码'" + park.getParkingLotCode() + "'所属停车场"+ park.getParkingLotName()+"车位"+ park.getCode()+"已存在");
            }}
        park.setCreateTime(DateUtils.getNowDate());
        park.setCreateBy(getUsername());
        return toAjax(parkService.addPark(park));
    }
    @PreAuthorize("@ss.hasPermi('park:park:edit')")
    @Log(title ="修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult uppark(@Validated @RequestBody csd_parking park)
    {

        String houseNum=park.getHouseNum();
        if(houseService.checkHouseNum(houseNum)==null)
        { return error("门牌号'"+houseNum+"'不存在");}
        else{
            String s1=park.getCode();
            String s2=park.getParkingLotCode();
            String s3=park.getParkingLotName();
            if(parkService.checcode(s1,s2,s3)!=null&&parkService.checcode(s1,s2,s3).getId()!=park.getId()){
                return error("新增车位,停车场编码'" + park.getParkingLotCode() + "'所属停车场"+ park.getParkingLotName()+"车位"+ park.getCode()+"已存在");
            }}
        return toAjax(parkService.updatePark(park));
    }
    @PreAuthorize("@ss.hasPermi('park:park:query')")
    @GetMapping("/query/{id}")
    public AjaxResult authRole(@PathVariable("id") int id)
    {
        AjaxResult ajax = AjaxResult.success();
        csd_parking park = parkService.selectParkInfoById(id);
        ajax.put("park", park);
        return ajax;
    }

}
